<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" ref="queryRef" >
      <el-form-item label="展示名称" prop="displayName">
        <el-input
            v-model="queryParams.displayName"
            placeholder="请输入展示名称"
            clearable
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="processList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="显示名称" align="center" prop="displayName" />
      <el-table-column label="唯一编码" align="center" prop="name" />
      <el-table-column label="流程分类" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="wf_process_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="是否已部署" align="center" prop="isDeployed">
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.isDeployed" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDesign(scope.row)">设计</el-button>
        </template>
      </el-table-column>
    </el-table>
    <ProcessDesignModal :record="record" ref="designModalRef"></ProcessDesignModal>
    <ProcessViewerModal :record="record" ref="viewerRef"></ProcessViewerModal>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog :title="editTitle" v-model="editFlag" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="userRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <el-tree-select v-model="form.deptId" :data="enabledDeptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择归属部门" check-strictly />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="editFlag=false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="processDesign">
import { ref, onMounted, defineProps, defineExpose, nextTick } from 'vue'
import ProcessDesignModal from './ProcessDesignModal.vue'
import ProcessViewerModal from './ProcessViewerModal.vue'
import { listProcess, wfProcessDesignDetail } from "@/api/wf/processDesign.js";
import a from '@/utils/request'
// import demojson from './demo.json'
const { proxy } = getCurrentInstance();
const { wf_process_type } = proxy.useDict("wf_process_type");
const { yes_no } = proxy.useDict("yes_no");
const editTitle = "流程设计基本信息修改"

const processList = ref([]);
const total = ref(0);
const loading = ref(true);
// 设计模式
const processDesignVisible = ref(false)
const record = ref(null)
const editFlag = ref(false)

// 声明一个 ref 来存放该元素的引用
const designModalRef = ref(null)
const viewerRef = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // 显示名称
    displayName: undefined,
    // 唯一编码
    name: undefined
  },
  rules: {
    displayName: [{ required: true, message: "显示名称不能为空", trigger: "blur" }],
    name: [{ required: true, message: "唯一编码不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询流程列表 */
function getList() {
  loading.value = true;
  listProcess(queryParams.value).then(response => {
    console.log('processDesign response', response)
    processList.value = response.rows;
    total.value = response.recordCount;
  }).finally(() => {
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
}

/** 查看按钮操作 */
function handleView(row) {
  // 查询流程设计详情信息
  loading.value = true;
  record.value = row
  wfProcessDesignDetail({
    id: row.id+""
  }).then(response => {
    const flowData = response.data.jsonObject || {}
    console.log('flowData is ', flowData)
    record.value = row
    viewerRef.value.show(flowData)
  }).finally(() => {
    loading.value = false
  })

}

/** 修改按钮操作 */
function handleEdit() {
  editFlag.value = true
}
/** 设计按钮操作 */
function handleDesign(row) {
  wfProcessDesignDetail({
    id: row.id+""
  }).then(response => {
    const flowData = response.data.jsonObject || {}
    console.log('flowData is ', flowData)
    record.value = row
    designModalRef.value.show(flowData)
  }).finally(() => {
    loading.value = false
  })
}

/** 表单重置 */
function reset() {
  form.value = {
    displayName: undefined,
    name: undefined
  };
  proxy.resetForm("postRef");
}

getList();
</script>
<style scoped>

</style>
